/*
Novesfora Interactive Design Studio
Goiânia,Brazil
+55 62 8124.1712 ( Pablo Davi )
social@novesfora.com.br
p@novesfora.com.br - Pablo Davi > Flash Designer and Actionscript Developer

- 10 Ago '09 : Creating the 0.1 version of ScrollBase
	- Static Function to add ScrollBase ( without create object )
	- No External Tween needed 
	
- 16 Sep '09 : Creating Stage access class v0.1 for ScrollBase
	- novesfora.utils.StageLocation ( get and set stage )
	this update give access to stage for ScrollBase, you can
	use it in loaded file and use stage properties.
	- Added TweenLite effect - Download on: http://blog.greensock.com/tweenliteas3/
	Version: 10.09, Updated 3/20/2009
	Compatibility: Flash Player 9 and later (ActionScript 3)

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are
  met:

  * Redistributions of source code must retain the above copyright notice, 
    this list of conditions and the following disclaimer.
  
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the 
    documentation and/or other materials provided with the distribution.
  
  * Neither the name of Novesfora, DES84 nor the names of its 
    contributors may be used to endorse or promote products derived from 
    this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package Born.scroll
{
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import Born.utils.StageLocation;

	public class ScrollBase extends MovieClip
	{
		private static var Content					:MovieClip;
		private static var Mask						:MovieClip;
		private static var Dragger					:MovieClip;
		private static var Bar						:MovieClip;
		private static var Time						:Number;		
		private static var finalY					:Number;
		private static var actualY					:Number;
		private static var Percent					:uint;
		private static var Padding					:uint;
		private static var isScaling				:Boolean;
		
		public function ScrollBase(content:MovieClip, mask:MovieClip, dragger:MovieClip, bar:MovieClip, scalingDragger:Boolean = false, time:Number = 0.5, padding:uint = 5)
		{
			if ( content == null || mask == null || dragger == null || bar == null ) return
			// Set MovieClips
			Content 		= content;
			Mask 			= mask;
			Dragger 		= dragger;
			Bar 			= bar;
			isScaling		= scalingDragger;
			Time			= time;
			Padding			= padding;
			Content.mask 	= Mask;
			Dragger.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
			if ( StageLocation.getStage == null ) 
			{
				Bar.addEventListener(MouseEvent.CLICK, moveDragOnClick);
				Dragger.stage.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
			}
			else (StageLocation.getStage as Stage).addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
			verifyScrollBase();
		}
		/**
		 * When MouseDown will Drag scroll and Movecontent
		 * @param e
		 */		
		private function onDrag(e:MouseEvent):void
		{
			Dragger.startDrag(false, new Rectangle(
								Bar.x,
								Bar.y,
								0,
								Bar.height - Dragger.height) // End of Rectangle
							); // End Of StartDrag
							
			if ( StageLocation.getStage == null ) Dragger.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMoveDragger);
			else (StageLocation.getStage as Stage).addEventListener(MouseEvent.MOUSE_MOVE, onMoveDragger);
		}
		/**
		 * Move dragger to Mouse position on click event<br />
		 * and will move the content in the same time 
		 * @param e
		 */		
		private function moveDragOnClick(e:MouseEvent):void
		{
			Dragger.y = mouseY;
			if ( Dragger.y + Dragger.height > Bar.y + Bar.height ) Dragger.y = ( Bar.y + Bar.height ) - Dragger.height;
			onMoveDragger(null);
		}
		/**
		 * Moving the content when press Dragger 
		 * @param e
		 */		
		private function onMoveDragger(e:MouseEvent):void
		{
			Percent = (100 / uint((Bar.height - Dragger.height))) * (Dragger.y - Bar.y);
			finalY 	= Mask.y - (((Content.height - Mask.height + Padding)/100)*Percent);
			
			// Content.y = finalY;
			TweenLite.to(Content, Time * 1.5, { y : finalY, ease : Expo.easeOut });
			verifyScrollBase();
		}
		/**
		 * Verify if is necessary the scroll or not <br />
		 * And wil change the height of the dragger depending <br />
		 * of the content - mask proportion
		 */		
		public static function verifyScrollBase():void
		{
			if ( Content.height < Mask.height )
			{
				Bar.visible = false;
				Dragger.visible = false;
				TweenLite.to(Content,Time * 1.5, { y : Mask.y });
			}
			else
			{
				Bar.visible = true;
				Dragger.visible = true;
				if ( isScaling ) TweenLite.to(Dragger, Time * 0.5, { height : ( Mask.height / Content.height ) * Bar.height });
			}
		}
		/**
		 * Stoping All Drag Event 
		 * @param e
		 */		
		private function onStopDrag(e:MouseEvent):void
		{
			Dragger.stopDrag();
			if ( StageLocation.getStage == null ) Dragger.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMoveDragger);
			else (StageLocation.getStage as Stage).removeEventListener(MouseEvent.MOUSE_MOVE, onMoveDragger);
		}
		/**
		 * Will name and delegate functions to all movieclips inside the ScrollBase Function<br />
		 * All the movieclips will be send to __constructor.
		 * @param $content MovieClip of the content
		 * @param $mask Mask that will be on $content
		 * @param $dragger Is the Clip that will be moved
		 * @param $bar Dragger will be moved upside this
		 * 
		 */		
		public static function add(content:MovieClip, mask:MovieClip, dragger:MovieClip, bar:MovieClip, scalingDragger:Boolean = false, time:Number = 0.5, padding:uint = 5 ):ScrollBase
		{			
			return new ScrollBase(content, mask, dragger, bar, scalingDragger, time, padding); // Retun all data to _constructor
		}
		/**
		 * When the ScrollBase will be removed <br />
		 * will remove all the listeners and dynamic objects 
		 * @param e :Event
		 */		
		private function onRemove(e:Event):void
		{
			Bar.removeEventListener(MouseEvent.CLICK, moveDragOnClick);
			Dragger.removeEventListener(MouseEvent.MOUSE_DOWN, onDrag);
			if ( StageLocation.getStage == null ) Dragger.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMoveDragger);
			else (StageLocation.getStage as Stage).removeEventListener(MouseEvent.MOUSE_MOVE, onMoveDragger);
		}
		
	} // End of Class
	
} // End of Package